package com.its.web;

import com.its.util.Result;
import com.its.util.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 验证码
 */
@RestController
public class VerifyCodeController {
  @Autowired
  private RedisTemplate redisTemplate;
  @GetMapping("/verify")
    public Result verify(HttpServletResponse response,HttpServletRequest request) throws IOException {
      VerifyCode verifyCode = new VerifyCode();
      verifyCode.drawImage(response.getOutputStream());
      String code = verifyCode.getCode();
      // 存redis
      String addr = request.getRemoteAddr();// 获取本机的ip地址,IPV6格式 但是这里可能会获取奇怪的回环地址。
      System.out.println("addr:"+addr+"code"+code);
      redisTemplate.opsForValue().set(addr,code,5, TimeUnit.MINUTES);
      return null;
  }
}